#include "bossplay.h"

BossPlay::BossPlay(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口
    //固定尺寸
    this->setFixedSize(mainSceneWidth,mainSceneHeight);
    //标题
    this->setWindowTitle(mainSceneName);
    //图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    PushButton_01 * back =new PushButton_01(":/res/BackButton.png",":/res/BackButtonSelected.png");
    back->setParent(this);
    back->move(this->width() - back->width(),this->height() - back->height());
    connect(back,&PushButton_01::clicked,[=](){
        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->BossSceneBack();
        });
    });

    //当前关卡标题
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level:Unbelievable ");
    label->setText(str);
    label->setGeometry(QRect(30,this->height() - 50, this->width(),50 ));
    label->setAttribute(Qt::WA_TransparentForMouseEvents);

    //当前步数
    QLabel * plabel = new QLabel;
    plabel->setParent(this);
    QFont pfont;
    pfont.setFamily("华文新魏");
    pfont.setPointSize(20);
    plabel->setFont(pfont);
    QString pstr = QString("pace: %1").arg(this->pace);
    plabel->setText(pstr);
    plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
    plabel->setAttribute(Qt::WA_TransparentForMouseEvents);


    //初始化二维数组
    int array21[19][19];
    srand((unsigned)time(NULL));
    for(int i=0;i<19;i++)
    {
        for(int j=0;j<19;j++)
        {
            array21[i][j] = (rand() % 2);
            //array21[i][j]=1;
            //if(i==18&&j==18) array21[i][j]=0;
        }
    }
    for(int i=0;i<19;i++){
        for(int j=0;j<19;j++){
            this->gameArray[i][j] = array21[i][j];
        }
    }

    //创建金币的背景图片
    for(int i = 0 ; i < 19;i++)
    {
        for(int j = 0 ; j < 19; j++)
           {
            //绘制背景图片
            QLabel* label = new QLabel;
            QPixmap pix;
            pix.load(":/res/BoardNode(1).png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(35 + i*50,40+j*50);

            //创建金币
            QString str;
            if(this->gameArray[i][j] == 1){
                str = ":/res/Coin0001.png";
            }
            else
            {
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(35 + i*50,40+j*50);
            coin->posX=i;
            coin->posY=j;
            coin->flag = gameArray[i][j];
            this->coinBtn[i][j]=coin;
            //点击硬币调用
            connect(coin,&MyCoin::clicked,[=](){

                if(current==0)
                {
                    coin->changeFlag();

                    //刷新步数显示
                    pace++;
                    QString pstr = QString("pace: %1").arg(this->pace);
                    plabel->setText(pstr);
                    plabel->setGeometry(QRect(30,this->height() - 100, this->width(),50 ));
                    plabel->setAttribute(Qt::WA_TransparentForMouseEvents);


                    //同步二维数组，方便后期扩展
                    gameArray[i][j]=gameArray[i][j]==0?1:0;

                    //翻转周围硬币
                    QTimer::singleShot(300,this,[=](){
                        //右侧
                        if(coin->posX+1<=18)
                        {
                            this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                            gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]==0?1:0;
                        }
                        //左侧
                        if(coin->posX-1>=0)
                        {
                            this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                            gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]==0?1:0;
                        }
                        //下侧
                        if(coin->posY+1<=18)
                        {
                            this->coinBtn[coin->posX][coin->posY+1]->changeFlag();
                            gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]==0?1:0;
                        }
                        //上侧
                        if(coin->posY-1>=0)
                        {
                            this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                            gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==0?1:0;
                        }
                    });
                }
            });

        }
    }

}
//绘图事件
void BossPlay::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bfda0be809541c57e1407952d1e6042.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}
